package com.weipeep.sys.controller;


import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.weipeep.sys.domain.ClassTypeDO;
import com.weipeep.sys.vo.ValueTextVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weipeep.sys.domain.ChannelTypeDO;
import com.weipeep.sys.service.ChannelTypeService;
import com.weipeep.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-08-04 17:26:05 | wangyun</small>
 */
@Controller
@RequestMapping("/sys/channelType")
public class ChannelTypeController {
	@Autowired
	private ChannelTypeService channelTypeService;
	
	@GetMapping()
	@RequiresPermissions("sys:channelType:channelType")
	String ChannelType(){
	    return "sys/channelType/channelType";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sys:channelType:channelType")
	public Result<Page<ChannelTypeDO>> list(Integer pageNumber, Integer pageSize, ChannelTypeDO channelTypeDTO){
		// 查询列表数据
        Page<ChannelTypeDO> page = new Page<>(pageNumber, pageSize);
        
        Wrapper<ChannelTypeDO> wrapper = new QueryWrapper<>(channelTypeDTO);
        page = (Page<ChannelTypeDO>) channelTypeService.page(page, wrapper);
        return Result.ok(page);
	}

	@ResponseBody
	@GetMapping("/list/dict")
	public Result<List<ValueTextVo>> list(ChannelTypeDO channelTypeDO){
		// 查询列表数据
		Wrapper<ChannelTypeDO> wrapper = new QueryWrapper(channelTypeDO);
		List<ChannelTypeDO> channelTypeDOList =  channelTypeService.list(wrapper);
		List<ValueTextVo> valueTextVoList  = Lists.newArrayList();
		channelTypeDOList.stream().forEach(channelTypeDOTemp -> {
			ValueTextVo valueTextVo = new ValueTextVo();
			valueTextVo.setValue(channelTypeDOTemp.getId());
			valueTextVo.setTitle(channelTypeDOTemp.getTypeName());
			valueTextVoList.add(valueTextVo);
		});
		return Result.ok(valueTextVoList);
	}

	@GetMapping("/add")
	@RequiresPermissions("sys:channelType:add")
	String add(Model model,ChannelTypeDO channelTypeDO){
		Wrapper<ChannelTypeDO> wrapper = new QueryWrapper(channelTypeDO);
		List<ChannelTypeDO> channelTypeDOList =  channelTypeService.list(wrapper);
		List<ValueTextVo> channelTypeDicList  = Lists.newArrayList();
		channelTypeDOList.stream().forEach(channelTypeDOTemp -> {
			ValueTextVo valueTextVo = new ValueTextVo();
			valueTextVo.setValue(channelTypeDOTemp.getId());
			valueTextVo.setTitle(channelTypeDOTemp.getTypeName());
			channelTypeDicList.add(valueTextVo);
		});
		model.addAttribute("channelTypeDicList",channelTypeDicList);
	    return "sys/channelType/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("sys:channelType:edit")
	String edit(@PathVariable("id") Long id,Model model,ChannelTypeDO channelTypeDO){
		Wrapper<ChannelTypeDO> wrapper = new QueryWrapper(channelTypeDO);
		List<ChannelTypeDO> channelTypeDOList =  channelTypeService.list(wrapper);
		List<ValueTextVo> channelTypeDicList  = Lists.newArrayList();
		channelTypeDOList.stream().forEach(channelTypeDOTemp -> {
			ValueTextVo valueTextVo = new ValueTextVo();
			valueTextVo.setValue(channelTypeDOTemp.getId());
			valueTextVo.setTitle(channelTypeDOTemp.getTypeName());
			channelTypeDicList.add(valueTextVo);
		});
		model.addAttribute("channelTypeDicList",channelTypeDicList);
		ChannelTypeDO channelType = channelTypeService.getById(id);
		model.addAttribute("channelType", channelType);
	    return "sys/channelType/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:channelType:add")
	public Result<String> save( ChannelTypeDO channelType){
		if(channelTypeService.save(channelType)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("sys:channelType:edit")
	public Result<String>  update( ChannelTypeDO channelType){
		channelTypeService.updateById(channelType);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("sys:channelType:remove")
	public Result<String>  remove( Long id){
		if(channelTypeService.removeById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("sys:channelType:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		channelTypeService.removeByIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
